<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="net.sf.json.JSONObject,weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
	BaseBean bb= new BaseBean();
	RecordSet rs = new RecordSet();
	RecordSet rs_mx = new RecordSet();
	RecordSet rs_x = new RecordSet();
	Map<String, String> dataMap = new HashMap<String, String>();
	bb.writeLog("====getLabel====");
	String status = request.getParameter("status");
	String diamert = request.getParameter("diamert");
	String customer = request.getParameter("customer");//客户
	String product = request.getParameter("product");
	diamert = getDiameter(diamert);
	String type = request.getParameter("type");
	product = getProduct(product);
	String ref = request.getParameter("ref");
	bb.writeLog("ref==="+ref);
	bb.writeLog("status==="+status);
	bb.writeLog("diamert==="+diamert);
	bb.writeLog("customer==="+customer);
	bb.writeLog("product==="+product);
	bb.writeLog("type==="+type);
	//String qzd = customer+diamert+product+"Q";
	String qzd = "";
	String lsh = "";
	String bbh = "";
	String flag = "0";
	String message = "";
	String bm = "";
	if(type.equals("0")){
		//新增
		qzd = customer+diamert+product+status;
		bb.writeLog("qzd=="+qzd);
		String sql_ishavels = "select 1 from uf_labelbm where qzd ='"+qzd+"'";
		bb.writeLog("sql——ishave=="+sql_ishavels);
		rs.execute(sql_ishavels);
		boolean ishave = false;
		if(rs.next()){
			ishave = true;
		}
		if(ishave){
			//之前存在流水
			String sql_max = "select max(lsh) as lsh  from uf_labelbm where qzd = '"+qzd+"'";
			bb.writeLog("sql_max=="+sql_max);
			rs.execute(sql_max);
			String lsh_max = "";
			if(rs.next()){
				lsh_max = Util.null2String(rs.getString("lsh"));
			}
			if(Integer.parseInt(lsh_max)==99){
				message  = "error!";
				flag = "1";
			}else{
				lsh = String.valueOf(Integer.parseInt(lsh_max)+1);
				if(lsh.length()==1){
					lsh = "0"+lsh;
				}
				bbh = "00";
				bm = qzd+lsh+bbh;
				message = "SUCCESS!";
			}
			
		}else{
			lsh = "00";
			bbh = "00";
			bm = qzd+lsh+bbh;
			message = "SUCCESS!";
		}		
		
	}else{
		//变更
			if(ref.length()>0){
				String first = ref.substring(0,6);
				String between = ref.substring(6,8);
				String last = ref.substring(8,10);
				String sql_bbh = "select max(bbh) as bbh from uf_labelbm where qzd='"+first+"' and lsh='"+between+"'";
				String bbh_max = "";
				bb.writeLog("sql_bbh=="+sql_bbh);
				rs.execute(sql_bbh);
				if(rs.next()){
					bbh_max = Util.null2String(rs.getString("bbh"));
				}
				if(Integer.parseInt(bbh_max)==99){
					message  = "error!";
					flag = "1";
				}else{
					String new_bbh = String.valueOf(Integer.parseInt(bbh_max)+1);
					if(new_bbh.length()==1){
						new_bbh = "0"+new_bbh;
					}
					lsh  = between;
					bbh = new_bbh;
					qzd = first;
					bm = qzd+lsh+bbh;
					message = "SUCCESS!";
				}
			}else{
				message = "请选择对应的编码之后再生成!";
				flag = "1";
			}
	}
	dataMap.put("qzd",qzd);
	dataMap.put("bm",bm);
	dataMap.put("bbh",bbh);
	dataMap.put("lsh",lsh);
	dataMap.put("message",message);
	dataMap.put("flag",flag);
	JSONObject json = JSONObject.fromObject(dataMap);
	out.print(json.toString());
%>
<%! 
public String getDiameter(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_dmtdzb where diameter='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}
public String getProduct(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_prodzb where producttype='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}

public String getlabel(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select labelname from uf_labelmana1 where id='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("labelname"));
	}
	return ret;
}
%>

	